package com.cdream.basic.algorithm.template;

import cn.hutool.core.util.RandomUtil;

import java.util.Arrays;

/**
 * 排序模板
 */
public class Sort {
    private static int[] EXAMPLE = new int[]{1, 4, 6, 7, 1, 2, 6,9,3};

    public static void main(String[] args) {
        Sort sort = new Sort();
        sort.quickSort(EXAMPLE,0,EXAMPLE.length-1);
        System.out.println(Arrays.toString(EXAMPLE));

    }

    public void quickSort(int[] arr, int start, int end) {

        if(start>=end){
            return;
        }
        swap(arr,start,start + (int) (Math.random() * (end - start)));

        int pivot = start;
        for (int i = start + 1; i <= end; i++) {
            if (arr[i] <= arr[start]) {
                swap(arr, ++pivot, i);
            }
        }
        swap(arr,start,pivot);
        quickSort(arr,start,pivot-1);
        quickSort(arr,pivot+1,end);

    }


    public void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
